package com.duoec.dw.lineage.service.analyser.commend;

import com.duoec.dw.lineage.dto.SqoopCommendArgument;
import com.duoec.dw.lineage.exceptions.LineageAnalyseException;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class HcatalogPartitionValuesAnalyser extends BaseCommendLineAnalyser {
    /**
     * 用于检查的起始字段串，如果命中后就会执行
     *
     * @return 起始字段串
     */
    @Override
    public String getStart() {
        return "hcatalog-partition-values ";
    }

    /**
     * 分析
     *
     * @param importDto 血缘关系数据
     * @param line      需要处理的行
     */
    @Override
    public void analyse(SqoopCommendArgument importDto, String line) {
        String partitionValues = line.substring(getStart().length()).trim();
        if (StringUtils.isEmpty(partitionValues)) {
            throw new LineageAnalyseException("无法解析: hcatalog-partition-keys，" + line);
        }
        List<String> values = Lists.newArrayList();
        Splitter.on(",")
                .trimResults()
                .omitEmptyStrings()
                .split(partitionValues)
                .forEach(values::add);
        importDto.setPartitionKeys(values);
        importDto.setPartitionValues(values);
    }
}
